/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-08-12
 * Time: 20:09
 */
public class Test {
    //leetocde 59 螺旋数组
    public int[][] generateMatrix(int n) {
        int[][] ret = new int[n][n];
        int start = 0;
        int count = 1;
        int i = 0;
        int j = 0;
        int loop = 0;
        while(loop++ < n/2) {
            for(j = start; j < n - loop; j++) {
                ret[start][j] = count++;
            }
            for(i = start; i < n-loop; i++) {
                ret[i][j] = count++;
            }
            for(; j > start; j--) {
                ret[i][j] = count++;
            }
            for(; i > start; i--) {
                ret[i][j] = count++;
            }
            start++;
        }
        if(n % 2 == 1) {
            ret[start][start] = count;
        }

        return ret;
    }
}
